package com.example.netactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

public class RateListActivitymain extends ListActivity {
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] list_data = {"one","two","three","four"};
        List<String> mylist = new ArrayList<>(100);
        for(int i=1;i<=100;i++){
            mylist.add("Item" +i);
        }
        //Adapter
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mylist);  //适配器arratadapter
        setListAdapter(adapter);
        //getListView().setListAdapter
        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg){
                if(msg.what==7){
                    Bundle bundle = (Bundle)msg.obj;
                    //更新列表数据
                    ArrayList<String>list2 = bundle.getStringArrayList("mylist");
                    ListAdapter adapter = new ArrayAdapter<String>(RateListActivitymain.this, android.R.layout.simple_list_item_1,list2);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        //启动线程获取数据
        Thread t = new Thread (new MyTask(handler));
        //t.start();  不启动子线程的情况下  5-8
    }
}


//定义map对象需要放在循环内，否则代码不会报错但无法正常循环放入数据